package com.cooriproject.basemanage.controller;

import com.cooriproject.basemanage.config.R;
import com.cooriproject.basemanage.entity.ItemInfo;
import com.cooriproject.basemanage.service.ItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

@Api(tags = "货品模块(联至user表会出现问题)")
@RestController
@RequestMapping("/api/item")
public class ItemController {
    @Resource
    private ItemService itemservice;
    @ApiOperation(value = "添加货物申请")
    @PostMapping("/itemInsertApplicant")
    public R itemInsertApplicant(String itemCode, String itemName, Integer itemMinNum, String itemBrand, String itemModel, String itemSpecifications, Integer itemType, Integer itemDisaudit, Integer itemApplicantUserId, Integer itemCon){
        int success = 0;
        try {
            success = itemservice.itemInsertApplicant(itemCode, itemName, itemMinNum, itemBrand, itemModel, itemSpecifications, itemType, itemDisaudit, itemApplicantUserId, itemCon);
        }
        catch (Exception e){
            return R.error_dynamic("添加申请失败（未定义异常）");
        }
        if(success==1){
            return R.ok();
        }
        else{
            return R.error_dynamic("添加申请失败（未定义错误）");
        }
    }
    @ApiOperation(value = "根据货物id查询货物信息")
    @PostMapping("/selectItemById")
    public R selectItemById(Integer itemId){
        List<ItemInfo> list = itemservice.selectByPrimaryKey(itemId);
        if(list.isEmpty()){
            return R.error_targetnotfound();
        }
        else {
            return R.ok().data("obj",list);
        }
    }
    @ApiOperation(value = "管理员删除货物")
    @PostMapping("/deleteItem")
    public R deleteItem(Integer itemId,Integer userId){
        int success = 0;
        try {
            success = itemservice.deleteByPrimaryKey(itemId,userId);
        }
        catch (Exception e){
            return R.error_dynamic("删除失败（未定义异常）");
        }
        if(success==1){
            return R.ok();
        }
        else if(success == -1){
            return R.error_dynamic("权限不足");
        }else {
            return R.error_dynamic("删除失败（未定义错误）");
        }
    }
    @ApiOperation(value = "管理员更新货物(有bug:不会报错但更新失败)")
    @PostMapping("/updateItem")
    public R deleteItem(Integer itemId,Integer userId,String itemCode, String itemName, Integer itemMinNum, String itemBrand, String itemModel, String itemSpecifications, Integer itemType, Integer itemDisaudit, Integer itemApplicantUserId, Integer itemApproverStatus,Integer itemApproverUserId,Integer itemUpdatedUserId, String itemCreatedTime,String itemUpdatedTime,Integer itemCon){
        int success = 0;
        try {
            success = itemservice.updateByPrimaryKeySelective(itemId, userId, itemCode, itemName, itemMinNum, itemBrand, itemModel, itemSpecifications, itemType, itemDisaudit, itemApplicantUserId, itemApproverStatus, itemApproverUserId, itemUpdatedUserId, itemCreatedTime, itemUpdatedTime, itemCon);
        }
        catch (Exception e){
            return R.error_dynamic("更新失败（未定义异常）");
        }
        if(success==1){
            return R.ok();
        }
        else if(success == -1){
            return R.error_dynamic("权限不足");
        }else {
            return R.error_dynamic("更新失败（未定义错误）");
        }
    }
    @ApiOperation(value = "货物列表")
    @PostMapping("/selectAll")
    public R selectAll(){
        List<ItemInfo> list = itemservice.selectAll();
        if(list.isEmpty()){
            return R.error_targetnotfound();
        }
        else {
            return R.ok().data("obj",list);
        }
    }

}
